package com.liu.lsm.livestock.controller;

import com.liu.lsm.livestock.entity.Livestock;
import com.liu.lsm.livestock.service.LivestockService;
import com.liu.lsm.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/livestock")
public class LivestockController {

    @Autowired
    LivestockService livestockService;


    /**
     * 条件查询牛羊
     * */

    @RequestMapping("/listAll")
    public List<Livestock> listAll(Livestock livestock){
        return  this.livestockService.listAll(livestock);
    }

    /**
     * 添加牛羊
     * */
    @RequestMapping("/addLivestock")
    public ResultObj addLivestock(Livestock livestock){
        try{
            this.livestockService.insert(livestock);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改牛羊
     *
     * */
    @RequestMapping("/updateLivestock")
    public ResultObj updateLivestock(Livestock livestock){
        try{
            this.livestockService.updateById(livestock);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除牛羊
     */
    @RequestMapping("/deleteLivestock")
    public ResultObj deleteLivestock(Integer livestockId){
        try{
            this.livestockService.deleteById(livestockId);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
